package com.hngc.dao;

import com.hngc.dao.entity.User;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {

    Connection conn = DBUtil.getConnection();
    PreparedStatement pstmt =null;
    ResultSet rs = null;
    /**
     * 查询所有用户
     *
     * @return
     */
    public List<User> getAllUser() {
        List<User> list = new ArrayList<>();

        String sql = "select * from stu";
        try {
            //预编译sql语句
            pstmt = conn.prepareStatement(sql);
            //执行查询，并将返回结果赋给结果集
            rs = pstmt.executeQuery();

            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setName(rs.getString("name"));
                user.setAge(rs.getInt("age"));
                user.setHiredate(rs.getString("hiredate"));

                list.add(user);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 登录页面
     * @param name
     * @param pwd
     * @return
     */
    public boolean userLogin(String name, String pwd) {
        boolean flag = false;
        String sql = "select * from stu where name = ? and pwd = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pwd);

            rs = pstmt.executeQuery();

            if (rs.next()) {
                //如果结果集的下一行有数据
                flag = true;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    public boolean userReg(User user){
        boolean flag = false;
                String sql="insert into stu(name,pwd,age) values (?,?,?)";
        try{
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,user.getName());
            pstmt.setString(2, user.getPwd());
            pstmt.setInt(3,user.getAge());
            int result =pstmt.executeUpdate();

            if (result > 0){
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    /**根据id获得用户信息
     *
     */
    public User getUserById(int id){
        User user = null;
        String sql ="select*from stu where id = ?";
        try{
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();

            if (rs.next()){
                user =new User(rs.getInt("id"),rs.getString("name"),rs.getString("pwd"),rs.getInt("age"),rs.getString("hiredate"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    public boolean updateUser(User user){
        boolean flag = false;
        String sql = "update stu set name = ?,pwd= ?,age= ? where id = ?";
        try{
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPwd());
            pstmt.setInt(3, user.getAge());
            pstmt.setInt(4, user.getId());

            int result = pstmt.executeUpdate();
            if (result > 0) {
                flag = true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }
    public boolean delUser(int id) {
        String sql = "delete from stu where id = ?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            int result = pstmt.executeUpdate();

            if (result > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}